//
//  BottomView.m
//  xpkc
//
//  Created by TopSageOSX on 14-8-27.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "BottomView.h"

@implementation BottomView{
    /**
     *  提示内存的标签
     */
    UILabel *usedLabel;
    UILabel *mediaLabel;
    UILabel *freeLabel;
    CGRect frame_used,frame_media,frame_free;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self creatBottomView];
    }
    return self;
}
/**
 *  创建底部视图
 */
-(void)creatBottomView{
    //创建控件
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.noteLabel = [[UILabel alloc]init];
    self.lineHorizontal =[[UILabel alloc]init];
    self.lineVertical =[[UILabel alloc]init];
    self.noteView = [[UIView alloc]init];
    usedLabel = [[UILabel alloc]init];
    mediaLabel = [[UILabel alloc]init];
    freeLabel = [[UILabel alloc]init];
    //设定位置
    CGRect left_frame,right_frame;
    CGRect note_frame,note_label_frame;
    CGRect lineH_frame,lineV_frame;
    CGRect noteV_frame;
    if (isPad) {
        //
        left_frame = RECT(-SCREEN_WIDTH/2, 49, SCREEN_WIDTH/2, 49);
        right_frame = RECT(SCREEN_WIDTH, 49, SCREEN_WIDTH/2, 49);
        note_frame = RECT(0, 49, SCREEN_WIDTH, 0);
        note_label_frame = RECT(0, 6, SCREEN_WIDTH, 43);
        noteV_frame = RECT(0, 49, SCREEN_WIDTH, 49);
        lineH_frame = RECT(SCREEN_WIDTH/2, 49, 0, 0);
        lineV_frame = RECT(SCREEN_WIDTH/2, 52, 0, 0);
    }else{
        left_frame = RECT(-SCREEN_WIDTH/2, 25, SCREEN_WIDTH/2, 49);
        right_frame = RECT(SCREEN_WIDTH, 25, SCREEN_WIDTH/2, 49);
        note_frame = RECT(0, 25, SCREEN_WIDTH, 25);
        note_label_frame = RECT(0, 6, SCREEN_WIDTH, 19);
        noteV_frame = RECT(0, 25, SCREEN_WIDTH, 25);
        lineH_frame = RECT(SCREEN_WIDTH/2, 25, 0, 0);
        lineV_frame = RECT(SCREEN_WIDTH/2, 28, 0, 0);
    }
    frame_used = RECT(0, 0, 0, 6);
    frame_media = RECT(0, 0, 0, 6);
    frame_free = RECT(0, 0, 0, 6);
    usedLabel.frame = RECT(0, 0, 0, 6);
    mediaLabel.frame = RECT(0, 0, 0, 6);
    freeLabel.frame = RECT(0, 0, 0, 6);
    self.leftBtn.frame = left_frame;
    self.rightBtn.frame = right_frame;
    self.noteLabel.frame = note_label_frame;
    self.lineHorizontal.frame = lineH_frame;
    self.lineVertical.frame = lineV_frame;
    //详细属性
    self.noteView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.95];
    self.leftBtn.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.8];
    self.rightBtn.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.8];
    self.lineHorizontal.backgroundColor = [UIColor lightGrayColor];
    self.lineVertical.backgroundColor = [UIColor lightGrayColor];
    usedLabel.backgroundColor = [UIColor redColor];
    mediaLabel.backgroundColor = [UIColor orangeColor];
    freeLabel.backgroundColor = GreenColor;
    [self.noteView addSubview:freeLabel];
    [self.noteView addSubview:mediaLabel];
    [self.noteView addSubview:usedLabel];
    [self.noteView addSubview:self.noteLabel];
    //添加
    [self addSubview:self.noteView];
    [self addSubview:self.leftBtn];
    [self addSubview:self.rightBtn];
    [self addSubview:self.lineHorizontal];
    [self addSubview:self.lineVertical];
}
/**
 *  出现的动画
 *
 *  @param time  持续时间
 *  @param delay 延时
 */
-(void)showAnimationWithTime:(CGFloat)time andDely:(CGFloat)delay{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:time-0.4];
    [UIView setAnimationDelay:delay];
    //按钮
    CGRect left = self.leftBtn.frame;
    CGRect right = self.rightBtn.frame;
    left.origin.x = 0;
    right.origin.x = SCREEN_WIDTH/2;
    self.leftBtn.frame = left;
    self.rightBtn.frame = right;
    [UIView commitAnimations];
    [self performSelector:@selector(showNote) withObject:nil afterDelay:time-0.4];
}
/**
 *  标签和线
 */
-(void)showNote{
    CGRect noteV = self.noteView.frame;
    CGRect lineH = self.lineHorizontal.frame;
    CGRect lineV = self.lineVertical.frame;
    self.noteLabel.hidden = NO;
    if (isPad) {
        noteV.origin.y = 0;
        noteV.size.height = 49;
    }else{
        noteV.origin.y = 0;
        noteV.size.height = 25;
    }
    lineV.size.height = 40;
    lineV.size.width = 1;
    lineH.origin.x = 0;
    lineH.size.width = SCREEN_WIDTH;
    lineH.size.height = 1;
    self.hidden = NO;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4];
    self.noteView.frame = noteV;
    self.lineHorizontal.frame = lineH;
    self.lineVertical.frame = lineV;
    [UIView commitAnimations];
    
}
/**
 *  隐藏底部视图
 *
 *  @param time  持续时间
 *  @param delay 延时
 */
-(void)hideAnimationWithTime:(CGFloat)time andDely:(CGFloat)delay{
    [UIView animateWithDuration:time delay:delay options:UIViewAnimationOptionAllowAnimatedContent animations:^{
        //按钮
        CGRect left = self.leftBtn.frame;
        CGRect right = self.rightBtn.frame;
        left.origin.x = -SCREEN_WIDTH/2;
        right.origin.x = SCREEN_WIDTH;
        self.leftBtn.frame = left;
        self.rightBtn.frame = right;
        [UIView commitAnimations];
        //标签和线条
        CGRect noteV = self.noteView.frame;
        CGRect lineH = self.lineHorizontal.frame;
        CGRect lineV = self.lineVertical.frame;
        self.noteLabel.hidden = YES;
        if (isPad) {
            noteV.origin.y +=49;
            noteV.size.height = 0;
        }else{
            noteV.origin.y +=25;
            noteV.size.height = 0;
        }
        lineH.origin.x = SCREEN_WIDTH/2;
        lineH.size.width = 0;
        lineH.size.height = 0;
        lineV.size.height = 0;
        lineV.size.width = 0;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.2];
        self.noteView.frame = noteV;
        self.lineHorizontal.frame = lineH;
        self.lineVertical.frame = lineV;
    } completion:^(BOOL finished) {
        //
        self.hidden = YES;
    }];
}
/**
 *  设置提示标签的颜色
 *
 *  @param used  已用
 *  @param media 视频
 *  @param free  空闲
 */
-(void)setColorWith:(CGFloat)used andMedia:(CGFloat)media andFree:(CGFloat)free{
    //红橙绿
    //
    float usedSize = (used-media)/(used+free)*SCREEN_WIDTH;
    float mediaSize = media/(used+free)*SCREEN_WIDTH;
    float freeSize = free/(used+free)*SCREEN_WIDTH;
    //初始位置
    frame_used.origin.x = -usedSize;
    frame_media.origin.x = -usedSize-mediaSize;
    frame_free.origin.x = -usedSize-mediaSize-freeSize;
    frame_used.size.height = 6;
    frame_media.size.height = 6;
    frame_free.size.height = 6;
    frame_used.size.width = usedSize;
    frame_media.size.width = usedSize+mediaSize;
    frame_free.size.width = usedSize+mediaSize+freeSize;
    usedLabel.frame = frame_used;
    mediaLabel.frame = frame_media;
    freeLabel.frame = frame_free;
    [UIView animateWithDuration:1 delay:0.8 options:UIViewAnimationOptionOverrideInheritedDuration animations:^{
        frame_used.origin.x = 0;
        frame_media.origin.x = 0;
        frame_free.origin.x = 0;
        usedLabel.frame = frame_used;
        mediaLabel.frame = frame_media;
        freeLabel.frame = frame_free;
    } completion:nil];
    
}
-(void)hideColor{
    frame_used.size.height = 0;
    frame_media.size.height = 0;
    frame_free.size.height = 0;
    usedLabel.frame = frame_used;
    mediaLabel.frame = frame_media;
    freeLabel.frame = frame_free;
}
@end
